<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/youan/api/audit/eventRuleNum";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 1000,
          pageSizes:[1000],
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          labelWidth: 120,
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          addBtn: false,
          delBtn: false,
          editBtn: false,
          dialogClickModal: false,
          column: [
            {
              label: "规则ID",
              prop: "ruleId",
              rules: [{
                required: true,
                message: "请输入规则ID",
                trigger: "blur"
              }]
            },
            {
              label: "规则名称",
              prop: "ruleName",
              rules: [{
                required: true,
                message: "请输入规则名称",
                trigger: "blur"
              }]
            },
            {
              label: "说明",
              prop: "instructions",
              rules: [{
                required: true,
                message: "请输入说明",
                trigger: "blur"
              }]
            },
            {
              label: "源IP类型",
              prop: "sourceIpType",
              type: "select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=client_db",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请选择源IP类型",
                trigger: "blur"
              }]
            },
            {
              label: "目的IP类型",
              prop: "destinationIpType",
              type: "select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=client_db",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请选择目的IP类型",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    },
    activated() {
      this.$nextTick(() => {
        if (this.$refs.crud.$refs.table) {
          this.$refs.crud.$refs.table.doLayout();
        }
      })
    },
  };
</script>

<style>
</style>
